<?php
function inputBonito($name, $des,$type,$class)
{
    //  inputBonito("Backup_path", "Dirección física del respaldo","text","E:\\","col-md-4").
    $str ='<div class="form-group '.$class.'">';
    $str .= '<div class="row-fluid">';
    $str .=     Label($name);
    $str.=  '<input type="'.$type.'"placeholder="'.$des.'" id="id_'.$name.'">';
    $str.='</div></div>'; 
    return $str;
}  

function Label($valor)
{
    $str = '<span class="label label-primary">'.$valor.'</span>';
    return $str;
}

function Tabla($theads , $tbodys, $onclick,$id)
{  
    $cantCol=0;
    $cantIns=0;
    
    $str = '<div class="table-responsive divScroll"> ';
        $str .= '<table class="table table-striped table-fixed-header" id="'.$id.'">';//  table table-bordered table-fixed-header
            $str.= ' <thead class="header">';

            //lleno thead
            $resultado = count($theads);
             $bool = true;
            foreach($theads as $thead)
            {              
                if($bool)
                {
                    $str.= '<tr>';
                  $bool = false;
                }
                $str.='<th>'.$thead.'</th>';
                $cantCol++;
                if($cantCol == $resultado)
                {
                    $str.='</tr>';
                }
                
            }
            $str.='</thead>';

            //lleno tbody
            $str.='<tbody>';
            foreach ($tbodys as $tbody) {          
                if($cantIns==0)
                {
                    $cantIns++;
                    $str.='<tr onclick="'.$onclick.'" >';      
                    $str.='<td>'.$tbody.'</td>'; 
                                      
                 }elseif( $cantIns != $cantCol && $cantIns <= $cantCol)
                    {
                       $cantIns++;
                       $str.='<td>'.$tbody.'</td>'; 
                           if($cantIns == $cantCol)
                           {
                                $str.='</tr>';  
                                $cantIns = 0;
                           }
                    }                       
            }
            $str.='</tbody>';
        $str.='</table>';
    $str.='</div>';
    //echo ($str);
return  $str;
//$divPrincipal->echo ($str);
} 

function selectBox($opciones,$id,$labelName,$class)
{
    $str ='<div class="form-group '.$class.'">';
    $str .= '<div class="row-fluid">';
    $str .=     Label($labelName);
    $str .=     '<select class="selectpicker" id="'.$id.'" data-style="btn-primary">';
            foreach ($opciones as $value) 
                $str .= '<option>'.$value.'</option>';          
        $str.=  '</select>';
    $str .= '</div></div>';
    return $str; 
} 
 
function indexeando($arr,$target)
{  
    $count = 1;
    $c = "'";
    $val_ = Array(); 
    foreach ($arr as $v)
    {   
       // $val = json_encode(implode($v));
        $val = json_encode(implode($v));
        $td = "<span>".$val."</span>&nbsp<span class='glyphicon glyphicon-remove-sign' onclick='rmv(this,".$target.",".$val.")'></span>";
        array_push($val_,"".$count++);
        array_push($val_, $td);
    } 
    return $val_;
}

function  llenaCosas()
{   
    require('./php_procedures.php');
    $ctrls = procedure_call("CONTROLFILES");  
    $logs = procedure_call("LOGS");  
    $archives = procedure_call("ARCHIVELOGSS");  
    $spfile = procedure_call("SPFILE");    
    $tblspcs = procedure_call("DATAFILES");   
    $tblspcs_ = Array(); 
    $c = "'";
    foreach ($tblspcs as $t)
    { 
        $tb = $t["TABLESPACE_NAME"];
        array_push($tblspcs_,  "<span>".$tb."</span>");  
        $val = $t["FILE_NAME"];  
        $td = '<span>'.$val.'</span>&nbsp<span class="glyphicon glyphicon-remove-sign" onclick = "rmv(this,'.'sel_tbsp'.','.$c."[".$tb.",".$val."]".$c.')"></span>';
        array_push($tblspcs_, $td);
    }
    $str = "";
    $str .=
    '<div class = "row">'.
        '<div class = "col-md-6">'.  
            '<div class = "row borderJ">'.
                selectBox(["DEFAULT"], "sel_tbsp", "Tablespaces", "col-md-6"). 
                '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow('."'sel_tbsp'".','."''".')"></span>'.
            '</div>'.
            Tabla(["Tablespace","Datafile"] , $tblspcs_, '""',"tabla_tbsp").
        '</div>'.
        '<div class = "col-md-6">'.  
            '<div class = "row borderJ">'.
                selectBox(["DEFAULT"], "sel_init", "Initfiles", "col-md-6").
                '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow('."'sel_init'".','."''".')"></span>'.
            '</div>'.
            Tabla(["Row","File"] , indexeando($spfile, 'sel_init'), '""',"tabla_init").
        '</div>'.
    '</div>'.

    '<div class = "row">'. 
        '<div class = "col-md-6">'.
            '<div class = "row borderJ">'.
                selectBox(["DEFAULT"], "sel_control", "Controlfiles", "col-md-6").
                '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow('."'sel_control'".','."''".')"></span>'.
            '</div>'.
            Tabla(["Row","File"] , indexeando($ctrls,"sel_control"), '""',"tabla_control").
        '</div>'. 
        '<div class = "col-md-6">'. 
            '<div class = "row borderJ">'.
                selectBox(["DEFAULT"], "sel_arc", "Archives", "col-md-6").
                '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow('."'sel_arc'".','."''".')"></span>'.
            '</div>'.
            Tabla(["Row","File"] , indexeando($archives, 'sel_arc'), '""',"tabla_arc").
        '</div>'.
    '</div>'. 

    '<div class = "row">'.
        '<div class = "col-md-6">'. 
            '<div class = "row borderJ">'.
                selectBox(["DEFAULT"], "sel_log", "Logfiles", "col-md-6").
                '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow('."'sel_log'".','."''".')"></span>'.
            '</div>'.
            Tabla(["Row","File"] , indexeando($logs, 'sel_log'), '""',"tabla_log").
        '</div>'. 
        '<div class = "col-md-6">'.
            '<div></div>'.
    
        '</div>'.
    '</div>';
    return $str;
}